﻿Public Class frmRandCheckRuleMaster
    Private Def_Rule As String
    Private Def_Tab1_Value As String
    Private Def_Tab2_Value As String = "1"
    Private Def_Tab1_Type As String
    Private Def_Tab2_Type As String
    Private Const DB As String = "DB"
    Private Const WB As String = "WB"
    Private Const MD As String = "MD"
    Private Const CAR As String = "0"
    Private Const CIVIL As String = "1"
    Private Const NORMAL As String = "正常"
    Private dtCivil As DataTable
    Private dtCar As DataTable
    Private selectedIndex As Integer

    Private Sub cmbProcCD_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbProcCD.SelectedIndexChanged
        If chkChanged() Then
            RemoveHandler cmbProcCD.SelectedIndexChanged, AddressOf cmbProcCD_SelectedIndexChanged
            cmbProcCD.SelectedIndex = selectedIndex
            AddHandler cmbProcCD.SelectedIndexChanged, AddressOf cmbProcCD_SelectedIndexChanged
            Return
        End If

        If cmbProcCD.Text = DB Then
            setDBTab()
        ElseIf cmbProcCD.Text = WB Then
            setWBtab()
        ElseIf cmbProcCD.Text = MD Then
            setMDTab()
        End If
        selectedIndex = cmbProcCD.SelectedIndex
        'btnSearch.PerformClick()
        Dim dt1 As DataTable = dgvCivil.DataSource
        Dim dt2 As DataTable = dgvCar.DataSource
        If IsNothing(dt1) = False Then dt1.Clear()
        If IsNothing(dt2) = False Then dt2.Clear()


    End Sub
    Private Sub setDBTab()

        dgvCivil.Columns("colProdName").ReadOnly = True
        dgvCivil.Columns("colPKG").ReadOnly = True
        dgvCivil.Columns("colCheckValue").ReadOnly = True
        dgvCivil.Columns("colEquipNo").ReadOnly = True

        dgvCivil.Columns("colProdName").DefaultCellStyle.BackColor = Color.Silver
        dgvCivil.Columns("colPKG").DefaultCellStyle.BackColor = Color.Silver
        dgvCivil.Columns("colCheckValue").DefaultCellStyle.BackColor = Color.Silver
        dgvCivil.Columns("colEquipNo").DefaultCellStyle.BackColor = Color.Silver

        Def_Rule = ""
        Def_Tab1_Type = ""
        Def_Tab2_Type = "料盒数"
        Def_Tab1_Value = "0"

    End Sub
    Private Sub setWBtab()
        dgvCivil.Columns("colProdName").ReadOnly = False
        dgvCivil.Columns("colPKG").ReadOnly = True
        dgvCivil.Columns("colCheckValue").ReadOnly = False
        dgvCivil.Columns("colEquipNo").ReadOnly = False

        dgvCivil.Columns("colProdName").DefaultCellStyle.BackColor = Color.White
        dgvCivil.Columns("colPKG").DefaultCellStyle.BackColor = Color.Silver
        dgvCivil.Columns("colCheckValue").DefaultCellStyle.BackColor = Color.White
        dgvCivil.Columns("colEquipNo").DefaultCellStyle.BackColor = Color.White

        Def_Rule = "品种别"
        Def_Tab1_Type = "料盒数"
        Def_Tab2_Type = "料盒数"
        Def_Tab1_Value = "2"

    End Sub
    Private Sub setMDTab()
        dgvCivil.Columns("colProdName").ReadOnly = True
        dgvCivil.Columns("colPKG").ReadOnly = False
        dgvCivil.Columns("colCheckValue").ReadOnly = False
        dgvCivil.Columns("colEquipNo").ReadOnly = False

        dgvCivil.Columns("colProdName").DefaultCellStyle.BackColor = Color.Silver
        dgvCivil.Columns("colPKG").DefaultCellStyle.BackColor = Color.White
        dgvCivil.Columns("colCheckValue").DefaultCellStyle.BackColor = Color.White
        dgvCivil.Columns("colEquipNo").DefaultCellStyle.BackColor = Color.White

        Def_Rule = "PKG别"
        Def_Tab1_Type = "LOT数"
        Def_Tab2_Type = "LOT数"
        Def_Tab1_Value = "1"


    End Sub
    Private Sub setDefault()
        colRuleName.DefaultCellStyle.NullValue = Def_Rule
        colType.DefaultCellStyle.NullValue = Def_Tab1_Type
        colCheckValue.DefaultCellStyle.NullValue = Def_Tab1_Value
        colCarFlag.DefaultCellStyle.NullValue = CIVIL
        colProcCD.DefaultCellStyle.NullValue = cmbProcCD.Text
        colCheckFreq.DefaultCellStyle.NullValue = NORMAL
        colNGNum.DefaultCellStyle.NullValue = 0

        colCheckType_car.DefaultCellStyle.NullValue = Def_Tab2_Type
        colCheckValue_car.DefaultCellStyle.NullValue = Def_Tab2_Value
        colCarflag_car.DefaultCellStyle.NullValue = CAR
        colProcCD_car.DefaultCellStyle.NullValue = cmbProcCD.Text
        colNGNum_car.DefaultCellStyle.NullValue = 0
    End Sub

    Private Sub dgvCivil_DefaultValuesNeeded(sender As Object, e As DataGridViewRowEventArgs) Handles dgvCivil.DefaultValuesNeeded
        e.Row.Cells("colRuleName").Value = Def_Rule
        'colRuleName.DefaultCellStyle.NullValue = Def_Rule
        e.Row.Cells("colType").Value = Def_Tab1_Type
        'colType.DefaultCellStyle.NullValue = Def_Tab1_Type
        e.Row.Cells("colCheckValue").Value = Def_Tab1_Value
        'colCheckValue.DefaultCellStyle.NullValue = Def_Tab1_Value
        e.Row.Cells("colCarFlag").Value = CIVIL
        'colCarFlag.DefaultCellStyle.NullValue = CIVIL
        e.Row.Cells("colProcCD").Value = cmbProcCD.Text
        'colProcCD.DefaultCellStyle.NullValue = cmbProcCD.Text
        e.Row.Cells("colNGNum").Value = 0

        e.Row.Cells("colCheckFreq").Value = NORMAL

    End Sub


    Private Sub dgvCar_DefaultValuesNeeded(sender As Object, e As DataGridViewRowEventArgs) Handles dgvCar.DefaultValuesNeeded
        e.Row.Cells("colCheckType_car").Value = Def_Tab2_Type
        'colCheckType_car.DefaultCellStyle.NullValue = Def_Tab2_Type
        e.Row.Cells("colCheckValue_car").Value = Def_Tab2_Value
        'colCheckValue_car.DefaultCellStyle.NullValue = Def_Tab2_Value
        e.Row.Cells("colCarflag_car").Value = CAR
        'colCarflag_car.DefaultCellStyle.NullValue = CAR
        e.Row.Cells("colProcCD_car").Value = cmbProcCD.Text
        'colProcCD_car.DefaultCellStyle.NullValue = cmbProcCD.Text
        e.Row.Cells("colNGNum_car").Value = 0

        e.Row.Cells("colCheckNum_car").Value = 1
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        Dim objDBMSTRandCheck As New clsDBMSTRandCheck
        If chkChanged() Then Exit Sub

        If cmbProcCD.Text <> "" Then
            '民用
            dtCivil = objDBMSTRandCheck.selectRule(cmbProcCD.Text, CIVIL)


            If dtCivil.Rows.Count = 0 Then
                dtCivil.NewRow()
                ' MsgBox(getMsgStr("MSG10048") & "（民用品）")
                dgvCivil.DataSource = dtCivil
            Else
                dgvCivil.DataSource = dtCivil
            End If


            '車用
            dtCar = objDBMSTRandCheck.selectRule(cmbProcCD.Text, CAR)
            dgvCar.DataSource = dtCar
            If dtCar.Rows.Count = 0 Then
                dtCar.NewRow()
                ' MsgBox(getMsgStr("MSG10048") & "（车品）")
                dgvCar.DataSource = dtCar
            Else
                dgvCar.DataSource = dtCar

            End If
            setDefault()
        Else
            MsgBox(getMsgStr("MSG00017", lblProcCD.Text))
        End If
    End Sub
    Private Function chkChanged() As Boolean
        Dim dt1 As DataTable

        If Not IsNothing(dgvCivil.DataSource) Then
            dt1 = dgvCivil.DataSource
            If Not IsNothing(dt1.GetChanges) Then
                MsgBox(getMsgStr("MSG10055"))
                Return True
            End If
        End If
        If Not IsNothing(dgvCar.DataSource) Then
            dt1 = dgvCar.DataSource
            If Not IsNothing(dt1.GetChanges) Then
                MsgBox(getMsgStr("MSG10055"))
                Return True
            End If
        End If
        Return False
    End Function
    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        Dim objDBMSTRandCheck As New clsDBMSTRandCheck

        If objDBMSTRandCheck.updateMSTData(dtCivil, dtCar) Then
            MsgBox(getMsgStr("MSG00019", "数据库"))
            If IsNothing(dtCivil) = False Then
                dtCivil.AcceptChanges()
            End If
            If IsNothing(dtCar) = False Then
                dtCar.AcceptChanges()
            End If
            btnSearch.PerformClick()

        Else
            MsgBox(getMsgStr("MSG00009", "数据库保存"))
        End If

    End Sub

    Private Sub frmRandCheckRuleMaster_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        If chkChanged() Then e.Cancel = True
    End Sub
End Class